import { expect } from '@jest/globals';
import * as compressing from 'compressing';
import fsExtra from 'fs-extra';
import path from 'path';

import { buildApp } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.EmptyAbility]: 1
  }
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  await buildApp(project, taskConfig, record);
  const projectRootPath = project.projectRootPath();

  // 解析app包的内容
  await fsExtra.copyFile(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned.app'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned.zip'));
  await compressing.zip.uncompress(
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned.zip'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned'));

  // 解析hap包的内容
  await fsExtra.copyFile(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default.hap'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default.zip'));
  await compressing.zip.uncompress(
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default.zip'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default'));

  await fsExtra.copyFile(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default.hap'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default.zip'));
  await compressing.zip.uncompress(
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default.zip'),
    path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default'));

  const mainAbilityFiles = await fsExtra.readdir(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default/assets/js/MainAbility'));
  const pagesFiles = await fsExtra.readdir(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/application-entry-default/assets/js/MainAbility/pages'));
  const isBoolJs = mainAbilityFiles.every(item => item.includes('.js') || item.includes('js.map')) || pagesFiles.every(item => item.includes('.js') || item.includes('js.map'));
  expect(isBoolJs).toBe(false);

  const entryMainAbilityFiles = await fsExtra.readdir(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default/assets/js/MainAbility'));
  const entryPagesFiles = await fsExtra.readdir(path.resolve(projectRootPath, 'build/outputs/default/myapplication-default-unsigned/entry-default/assets/js/MainAbility/pages'));
  const isEntryBoolJs = entryMainAbilityFiles.every(item => item.includes('.js') || item.includes('js.map')) || entryPagesFiles.every(item => item.includes('.js') || item.includes('js.map'));
  expect(isEntryBoolJs).toBe(false);
};

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_ZOS_API10_FA_Ets_Increment_Build_002')
  .description('Release模式常规编译')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
